﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{

	public class UserDAO:IData<User> 
 	{
		public UserDAO(String myconnecString) : base(myconnecString) { }

		protected override int InsertOnSubmit(ref User item, int t) {
			 SqlParameter p = new SqlParameter(); 
			 if( t == 0)
				p.Direction = ParameterDirection.Output;
			 p.ParameterName = "@" + User.c_id;
			 p.Value = item.id;
			 Object obj = item.id; 
			int i = connec.ExecuteNonQuery("sp_User", CommandType.StoredProcedure,p,ref obj,new SqlParameter("@flag", t),
				 new SqlParameter("@" + User.c_user_name, item.user_name), new SqlParameter("@" + User.c_pass_word, item.pass_word),
				 new SqlParameter("@" + User.c_email, item.email), new SqlParameter("@" + User.c_phone, item.phone),
				 new SqlParameter("@" + User.c_type_id, item.type_id), new SqlParameter("@" + User.c_isdelete, item.isdelete),
				 new SqlParameter("@" + User.c_islock, item.islock), new SqlParameter("@" + User.c_create_date, item.create_date),
				 new SqlParameter("@" + User.c_loginend, item.loginend));
			if(t == 0)
				item.id = Convert.ToInt64(obj);
			return i;
		}

		public override User GetItem(IDataReader dr)
		{
			User obj = new User();
			obj.id = Convert.ToInt64(dr[User.c_id]);
			obj.user_name = Convert.ToString(dr[User.c_user_name]);
			obj.pass_word = Convert.ToString(dr[User.c_pass_word]);
			obj.email = Convert.ToString(dr[User.c_email]);
			obj.phone = Convert.ToString(dr[User.c_phone]);
			obj.type_id = Convert.ToInt64(dr[User.c_type_id]);
			obj.isdelete = Convert.ToBoolean(dr[User.c_isdelete]);
			obj.islock = Convert.ToBoolean(dr[User.c_islock]);
			obj.create_date = Convert.ToDateTime(dr[User.c_create_date]);
			obj.loginend = Convert.ToDateTime(dr[User.c_loginend]);
			return obj;
		}

		public override List<User> GetAll()
		{
			return GetAll("sp_GetAllUser", CommandType.StoredProcedure);
		}

		public override User GetItemById(object id)
		{
			return GetAll().SingleOrDefault(p => p.id == Convert.ToInt64(id));
		}
		public override User GetItemById(User item){
			throw new NotImplementedException();
		}

	}

 }
